package cn.itcast.web_07.session.example01;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ListBookServlet
 */
public class ListBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * 提供一个购买图书的页面
     */
    public ListBookServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 处理响应中文的乱码
	    response.setContentType("text/html;charset=utf-8");
	    // 1、得到所有图书
	    Collection<Book> allBooks = BookDB.getAll();
	    // 2、把图书回显到页面
	    PrintWriter out = response.getWriter();
	    out.print("本网站提供售卖的所有图书：" + "<br>");
	    for (Book book : allBooks) {
	        String id = book.getId();
	        String name = book.getName();
	        String url = "<a href = '/web_07/purcharse?id=" + id + "'>点击购买</a>";
	        out.print("图书名称：" + name + url + "<br>");
	    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
